<?php
require('./lib/init.php');

//计算分页代码
$sql = "select count(*) from art";
$sum = mGetOne($sql);//获取总文章数
$cnt = 3; //每页显示3篇文章
$curr = isset($_GET['page']) ? $_GET['page'] : 1 ;//当前页码数,从地址栏获取
$page = cPager($sum,$cnt,$curr);

if (isset($_GET['cat_id'])) {
	$where = "and art.cat_id='$_GET[cat_id]'";
} else {
	$where = '';
}
/********************************************************************************/
//判断有没有标签
if (isset($_GET['tags'])) {
	$where = "and art.tags = '$_GET[tags]'";
	/*$sql = "select count('tags') from art where tags = '$_GET[tags]'";
	$num = mGetOne($sql);*/
}else {
	$where = '';
}

//查询出某一标签下的文章,三表联查
$sql = "select art.art_id,art.title,art.pubtime,art.tags,art.content,art.comm,cat.catname from (art left join tag on art.tags = tag.tags) left join cat on art.cat_id = cat.cat_id where 1 ".$where." order by art_id desc";
$tag = mGetAll($sql);

/******************************************************************************/


//先判断有无cat_id,即是不是从首页再点击了栏目
if (isset($_GET['cat_id'])) {
	$where = "and art.cat_id= '$_GET[cat_id]'";
}else {
	$where = '';
}
//查询所有文章
$sql = "select * from art left join cat on art.cat_id = cat.cat_id where 1 ".$where." order by art_id desc limit ".($curr-1)*$cnt.','.$cnt;
$art = mGetAll($sql);
//print_r($art);
/********************************************************************************/
//若为空,跳回主页
if (empty($art)) {
	header("Location:index.php");
}
//查询近期文章
$sql = "select title,art_id from art order by art_id desc limit 0,5";
$arts = mGetAll($sql);
//print_r($arts);

//查询所有栏目
$sql = "select * from cat";
$cat = mGetAll($sql);
include(ROOT.'/view/front/index.html');


 ?>